﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class DropDownList_MoneyRequest : System.Web.UI.UserControl
    {
        public RadComboBox Child;
        int nOffset = 10;
        Main main = new Main();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_FIM_ReceiveDtls rd = new B_FIM_ReceiveDtls();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public RadComboBox GetComboBox()
        {
            RadComboBox ddl = ddlMoneyReqSeq;
            return ddl;
        }

        private void LoadData(RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strBgmYear = lblBgmYear.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;

            IList<M_FIM_MoneyRequests> infolist = (lblPetition.Text == "1" ?
                mr.GetPetitionList(strBgmSourceSeq, strBgmYear, strWorkAreaSeq, null, null, null, strFilter)
                : mr.GetList(strBgmSourceSeq, strBgmYear, strWorkAreaSeq, null, null, null, strFilter));
            ;
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + nOffset, infolist.Count);
            e.EndOfItems = endOffset == infolist.Count;

            //ddlContactSeq.Items.Clear();

            for (int i = itemOffset; i < endOffset; i++)
            {
                ddlMoneyReqSeq.Items.Add(new RadComboBoxItem(infolist[i].money_req_name, infolist[i].money_req_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infolist.Count);
        }

        public void LoadControl(string strBgmSourceSeq, string strBgmYear, string strWorkAreaSeq
            , bool bPetition, Nullable<int> nWidth, Nullable<int> nSetOffset)
        {
            if (nWidth != null)
            {
                ddlMoneyReqSeq.DropDownWidth = Unit.Pixel(nWidth.Value);
                ddlMoneyReqSeq.Width = Unit.Pixel(nWidth.Value);
            }

            if (nSetOffset != null)
                nOffset = nSetOffset.Value;

            lblBgmSourceSeq.Text = strBgmSourceSeq;
            lblBgmYear.Text = strBgmYear;
            lblWorkAreaSeq.Text = strWorkAreaSeq;
            lblPetition.Text = (bPetition ? "1" : "0");
        }

        public void SetSelectedValue(string strValue)
        {
            if (strValue == null)
            {
                strValue = "";
            }

            if (strValue.Length > 0)
            {
                lblMoneyReqSeq.Text = strValue;

                if (ddlMoneyReqSeq.FindItemByValue(strValue) == null)
                {
                    M_FIM_MoneyRequests info = mr.GetInfo(strValue);
                    ddlMoneyReqSeq.SelectedValue = strValue;
                    ddlMoneyReqSeq.Text = info.money_req_name;
                }
                else
                {
                    ddlMoneyReqSeq.SelectedValue = strValue;
                }
            }
            else
            {
                ddlMoneyReqSeq.SelectedIndex = -1;
                ddlMoneyReqSeq.Text = "";
            }

            ddlMoneyReqSeq_SelectedIndexChanged(null, null);
        }

        public string GetSelectedValue()
        {
            string strValue = lblMoneyReqSeq.Text;

            return strValue;
        }

        public void SetEnable(bool bEnable)
        {
            ddlMoneyReqSeq.Enabled = bEnable;
        }

        public void ddlMoneyReqSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string strMoneyReqSeq = ddlMoneyReqSeq.SelectedValue;
            lblMoneyReqSeq.Text = strMoneyReqSeq;

            if (Child != null)
            {

            }

            switch (Page.ToString())
            {
                case "ASP.app_ui_forms_fim_receives_aspx" :
                case "ASP.app_ui_forms_fim_receives2_aspx":
                    RadioButton rbContact2 = (RadioButton)Parent.FindControl("rbContact2");
                    RadioButton rbEmp2 = (RadioButton)Parent.FindControl("rbEmp2");
                    RadioButton rbTemp2 = (RadioButton)Parent.FindControl("rbTemp2");
                    Label lblContactSeq = (Label)Parent.FindControl("lblContactSeq");
                    Label lblEmpSeq = (Label)Parent.FindControl("lblEmpSeq");
                    Label lblContactPersonSeq = (Label)Parent.FindControl("lblContactPersonSeq");
                    Label lblContactName = (Label)Parent.FindControl("lblContactName");
                    Label lblEmpName = (Label)Parent.FindControl("lblEmpName");
                    Label lblContactPersonName = (Label)Parent.FindControl("lblContactPersonName");
                    Panel PanelBorrow = (Panel)Parent.FindControl("PanelBorrow");
                    Label lblAmount2 = (Label)Parent.FindControl("lblAmount2");
                    Label lblReqAmount = (Label)Parent.FindControl("lblReqAmount");
                    RadGrid grdMoneyRequestDtls = (RadGrid)Parent.FindControl("grdMoneyRequestDtls");
                    //Label lblReturnAmount = (Label)Parent.FindControl("lblReturnAmount");

                    if (strMoneyReqSeq.Length > 0)
                    {
                        M_FIM_MoneyRequests info = mr.GetInfo(strMoneyReqSeq);

                        if (info.debtor_contact_seq.ToString().Length > 0)
                        {
                            rbContact2.Checked = false;
                            rbEmp2.Checked = false;
                            rbTemp2.Checked = true;
                            lblContactPersonSeq.Text = info.debtor_contact_seq.ToString();
                            lblContactPersonName.Text = info.debtor_contact_name;
                        }
                        else if (info.contact_seq.ToString().Length > 0)
                        {
                            rbContact2.Checked = true;
                            rbEmp2.Checked = false;
                            rbTemp2.Checked = false;
                            lblContactSeq.Text = info.contact_seq.ToString();
                            lblContactName.Text = info.contact_name;
                        }
                        else
                        {
                            rbContact2.Checked = false;
                            rbEmp2.Checked = true;
                            rbTemp2.Checked = false;
                            lblEmpSeq.Text = info.emp_seq.ToString();
                            lblEmpName.Text = info.emp_name;
                        }

                        PanelBorrow.Visible = true;// info.event_seq.ToString() == "16";
                        if (PanelBorrow.Visible)
                        {
                            lblReqAmount.Text = (info.total_amount != null ? info.total_amount.Value.ToString("#,##0.00") : "0.00");
                            //lblReturnAmount.Text = (info.return_amount != null ? info.return_amount.Value.ToString("#,##0.00") : "0.00");
                            lblAmount2.Text = ((info.total_amount != null ? info.total_amount.Value : 0)
                                - (info.return_amount != null ? info.return_amount.Value : 0)).ToString("#,##0.00");
                        }

                        grdMoneyRequestDtls.DataSource = rd.GetReceiveDtlsAndMoneyRequestDtls(strMoneyReqSeq, null);
                        grdMoneyRequestDtls.DataBind();
                    }
                    else
                    {
                        lblContactSeq.Text = "";
                        lblEmpSeq.Text = "";
                        lblContactPersonSeq.Text = "";
                        lblContactName.Text = "";
                        lblEmpName.Text = "";
                        lblContactPersonName.Text = "";
                        lblReqAmount.Text = "";
                        //lblReturnAmount.Text = "";
                    }
                    break;

                case "ASP.app_ui_forms_fim_payments_old_aspx" :
                    Label lblPetitionName = (Label)Parent.FindControl("lblPetitionName");
                    Label lblPetitionSeq = (Label)Parent.FindControl("lblPetitionSeq");
                    Label lblPetitionDtlsSeq = (Label)Parent.FindControl("lblPetitionDtlsSeq");
                    Label lblEventSeq = (Label)Parent.FindControl("lblEventSeq");
                    Label lblBathAmount = (Label)Parent.FindControl("lblBathAmount");
                    Label lblPayName = (Label)Parent.FindControl("lblPayName");
                    Label lblPayee = (Label)Parent.FindControl("lblPayee");
                    Label lblEmpSeq2 = (Label)Parent.FindControl("lblEmpSeq");
                    Label lblContactSeq2 = (Label)Parent.FindControl("lblContactSeq");
                    Label lblContactPersonSeq2 = (Label)Parent.FindControl("lblContactPersonSeq");
                    RadGrid RadGridSalaryDeduct = (RadGrid)Parent.FindControl("RadGridSalaryDeduct");
                    RadTabStrip RadTabStrip1 = (RadTabStrip)Parent.FindControl("RadTabStrip1");

                    if (strMoneyReqSeq.Length > 0)
                    {
                        M_FIM_MoneyRequests info = mr.GetInfo(strMoneyReqSeq);
                        lblPetitionName.Text = info.petition_type_name;
                        lblPetitionSeq.Text = info.petition_seq.ToString();
                        lblPetitionDtlsSeq.Text = info.petition_dtls_seq.ToString();
                        lblEventSeq.Text = info.event_seq.ToString();
                        lblBathAmount.Text = info.total_amount_real.ToString();
                        lblPayName.Text = "จ่ายเงิน" + " (" + info.money_req_name + ")";
                        lblPayee.Text = info.payee_name;
                        lblEmpSeq2.Text = info.emp_seq.ToString();
                        lblContactSeq2.Text = info.contact_seq.ToString();
                        lblContactPersonSeq2.Text = info.debtor_contact_seq.ToString();

                        switch (lblEventSeq.Text)
                        {
                            case "11":
                                Label lblNetAmount = (Label)Parent.FindControl("lblNetAmount");
                                Label lblPenaltyAmount = (Label)Parent.FindControl("lblPenaltyAmount");
                                Label lblPercentVat = (Label)Parent.FindControl("lblPercentVat");
                                Label lblVatAmount = (Label)Parent.FindControl("lblVatAmount");
                                Label lblRedemptionAmount = (Label)Parent.FindControl("lblRedemptionAmount");
                                Label lblAmountNoVat = (Label)Parent.FindControl("lblAmountNoVat");
                                Label lblDeductionAmount = (Label)Parent.FindControl("lblDeductionAmount");
                                Label lblPercentTax = (Label)Parent.FindControl("lblPercentTax");
                                Label lblTaxAmount = (Label)Parent.FindControl("lblTaxAmount");

                                lblNetAmount.Text = info.total_amount.Value.ToString("#,###.##");
                                lblPenaltyAmount.Text = info.total_amount_penalty.Value.ToString("#,###.##");
                                lblPercentVat.Text = info.percent_vat.Value.ToString("#,###.##");
                                lblVatAmount.Text = info.total_amount_vat.Value.ToString("#,###.##");
                                lblRedemptionAmount.Text = info.total_amount_redemption.Value.ToString("#,###.##");
                                lblAmountNoVat.Text = (info.total_amount.Value - info.percent_vat.Value).ToString("#,###.##");
                                lblDeductionAmount.Text = info.total_amount_deduction.Value.ToString("#,###.##");
                                lblPercentTax.Text = info.percent_tax.Value.ToString("#,###.##");
                                lblTaxAmount.Text = info.total_amount_tax.Value.ToString("#,###.##");
                                break;
                            case "14":
                                RadGridSalaryDeduct.DataSource = mr.GetSalaryDeductList(strMoneyReqSeq);
                                RadGridSalaryDeduct.DataBind();
                                break;
                            case "12":
                            case "13":
                            case "15":
                            case "17":
                                Label lblPercentTax2 = (Label)Parent.FindControl("lblPercentTax2");
                                Label lblTaxAmount2 = (Label)Parent.FindControl("lblTaxAmount2");

                                lblPercentTax2.Text = info.percent_tax.Value.ToString("#,###.##");
                                lblTaxAmount2.Text = info.total_amount_tax.Value.ToString("#,###.##");
                                break;
                            default:
                                break;
                        }

                        RadTabStrip1.Tabs[1].Enabled = true;
                        RadTabStrip1.Tabs[2].Enabled = true;
                    }
                    else
                    {
                        lblPetitionName.Text = "";
                        lblPetitionSeq.Text = "";
                        lblPetitionDtlsSeq.Text = "";
                        lblEventSeq.Text = "";
                        lblBathAmount.Text = "0";
                        lblPayName.Text = "";
                        lblPayee.Text = "";
                        lblEmpSeq2.Text = "";
                        lblContactSeq2.Text = "";
                        lblContactPersonSeq2.Text = "";
                        RadGridSalaryDeduct.DataSource = null;
                        RadGridSalaryDeduct.DataBind();

                        RadTabStrip1.Tabs[1].Enabled = false;
                        RadTabStrip1.Tabs[2].Enabled = false;
                    }
                    break;

                case "ASP.app_ui_forms_fim_moneyreturns_aspx":
                    
                    RadGrid RadGrid2 = (RadGrid)Parent.FindControl("RadGrid2");
                    Label lblDebtor = (Label)Parent.FindControl("lblEmpName");
                    Label lblReturnSeq = (Label)Parent.FindControl("lblReturnSeq");
                    IBLL_FIM_MoneyReturnDtls red = new B_FIM_MoneyReturnDtls();
                    RadGrid2.DataSource = red.GetMoneyReqDtls(lblReturnSeq.Text, strMoneyReqSeq);
                    RadGrid2.DataBind();
                    lblDebtor.Text = mr.GetInfo(strMoneyReqSeq).payee_name;
                    break;
            }
        }

        protected void ddlMoneyReqSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadData(e);
        }
    }
}